﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-8-13
Description:CustomerList.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;

public partial class CustomerListSMSver : System.Web.UI.Page
{

    string TableName = "CustomerList";//表名
    static string SqlWhere = " 1=1 ";//过滤条件
    string GetCountSql = "select count(id)  from CustomerList where " + SqlWhere;
    string SqlTable = "select *  from CustomerList where " + SqlWhere + "  order by id desc ";
    string ReturnPage = "CustomerList.aspx";

    string SearchFilter = " and (CustomerName like '%{0}%' or customerAddress0 like '%{0}%' or memo like '%{0}%' or customerTel0 like '%{0}%' or customerwork like '%{0}%' or customerlandline0 like '%{0}%' )";

    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化模型

    protected void Page_Load(object sender, EventArgs e)
    {
        FTchina.basic.CheckAdmin(this);
        FTchina.basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        if (!IsPostBack)
        {
            string act = FTchina.basic.FilterSQL(Request["act"]);
            switch (act)
            {
                case "mod":
                    {
                        btnCustomerListOk.Text = "确定修改";
                        txbid.Enabled = false;
                        DisplayData();
                        plop.Visible = true;
                        break;
                    }
                case "del":
                    {
                        DeleteData();
                        break;
                    }
                case "add":
                    {
                        txbid.Text = "0";
                        txbid.Visible = false;
                        plop.Visible = true;//添加修改的表单可以看见
                        break;
                    }
                default:
                    {
                        txbid.Text = "0";
                        txbid.Visible = false;
                        plop.Visible = false;//添加修改的表单看不见
                        break;
                    }

            }

            if (qm.WorkPage != ReturnPage)
            {
                qm.TableName = TableName;
                qm.GetCountSql = GetCountSql;
                qm.GetTableSql = SqlTable;
                qm.ReturnPage = ReturnPage;
                qm.SqlWhere = SqlWhere;
                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGroup();
            bindData(qm);

        }
    }


    protected void BindGroup()
    {
        string sql = "select * from CustomerGroup order by id asc";
        using (DataTable dt = FTchina.shujuku.GetDataTable(sql))
        {
            ddlcg.DataSource = dt;
            ddlcg.DataTextField = "groupname";
            ddlcg.DataValueField = "id";
            ddlcg.DataBind();
            ddlpcg.DataSource = dt;
            ddlpcg.DataTextField = "groupname";
            ddlpcg.DataValueField = "id";
            ddlpcg.DataBind();
            ddlsearchcg.DataSource = dt;
            ddlsearchcg.DataTextField = "groupname";
            ddlsearchcg.DataValueField = "id";
            ddlsearchcg.DataBind();
            ddlsearchcg.Items.Add(new ListItem("==请选择==", "null"));
            ddlsearchcg.SelectedIndex = ddlsearchcg.Items.Count - 1;
        }

    }
    private void DeleteData()
    {
        FTchina.UImsg.PageRender(FTchina.BLL.CustomerListManager.DeleteCustomerListByid(Convert.ToInt32(FTchina.basic.FilterSQL(Request["id"]))) > 0, HttpContext.Current.Request.Path.ToString());
    }
    private void DisplayData()
    {
        FTchina.Models.CustomerList mCustomerList = FTchina.BLL.CustomerListManager.GetCustomerListByid(Convert.ToInt32(FTchina.basic.FilterSQL(Request["id"])));
        if (mCustomerList != null)
        {
            txbid.Text = mCustomerList.Id.ToString();

            txbcustomerName.Text = mCustomerList.CustomerName.ToString();

            this.ddlsex.SelectedValue = mCustomerList.CustormeSex.ToString();

            txbcustomerTel0.Text = mCustomerList.CustomerTel0.ToString();

            txbcustomerTel1.Text = mCustomerList.CustomerTel1.ToString();

            txbcustomerTel2.Text = mCustomerList.CustomerTel2.ToString();

            txbcustomerlandline0.Text = mCustomerList.Customerlandline0.ToString();

            txbcustomerlandline1.Text = mCustomerList.Customerlandline1.ToString();

            txbcustomerlandline2.Text = mCustomerList.Customerlandline2.ToString();

            txbcustomerAddress0.Text = mCustomerList.CustomerAddress0.ToString();

            txbcustomerMeetAddress.Text = mCustomerList.CustomerMeetAddress.ToString();
            txbcustomerWork.Text = mCustomerList.CustomerWork.ToString();
            txbmemo.Text = mCustomerList.Memo.ToString();
            if (mCustomerList.CustomerGroupID.ToString() != "0")
            { //默认批量导入设置为0，此时需要用户选择分组
                ddlcg.SelectedValue = mCustomerList.CustomerGroupID.ToString();
            }
            txbcallcount.Text = mCustomerList.Callcount.ToString();

            txbmeetCount.Text = mCustomerList.MeetCount.ToString();

            txblastCallDate.Text = mCustomerList.LastCallDate.ToString();

            txblastMeetDate.Text = mCustomerList.LastMeetDate.ToString();
        }
        else
        {
            UImsg.PageError("无数据！", HttpContext.Current.Request.Path.ToString());
        }
    }

    private void DataModifiy()
    {
        FTchina.Models.CustomerList mCustomerList = new FTchina.Models.CustomerList();

        mCustomerList.Id = Convert.ToInt32(FTchina.basic.FilterSQL(txbid.Text));

        mCustomerList.CustomerName = FTchina.basic.FilterSQL(txbcustomerName.Text);

        mCustomerList.CustormeSex = FTchina.basic.FilterSQL(ddlsex.SelectedValue);

        mCustomerList.CustomerTel0 = FTchina.basic.FilterSQL(txbcustomerTel0.Text);
        if ((!FTchina.basic.ValidateMobile(mCustomerList.CustomerTel0)) && mCustomerList.CustomerTel0.Length > 0)
        {
            FTchina.JS.Alert("手机号码输入错误！");
            return;
        }
        mCustomerList.CustomerTel1 = FTchina.basic.FilterSQL(txbcustomerTel1.Text);

        mCustomerList.CustomerTel2 = FTchina.basic.FilterSQL(txbcustomerTel2.Text);

        mCustomerList.Customerlandline0 = FTchina.basic.FilterSQL(txbcustomerlandline0.Text);

        mCustomerList.Customerlandline1 = FTchina.basic.FilterSQL(txbcustomerlandline1.Text);

        mCustomerList.Customerlandline2 = FTchina.basic.FilterSQL(txbcustomerlandline2.Text);

        mCustomerList.CustomerAddress0 = FTchina.basic.FilterSQL(txbcustomerAddress0.Text);
        mCustomerList.CustomerWork = FTchina.basic.FilterSQL(this.txbcustomerWork.Text);

        mCustomerList.CustomerMeetAddress = FTchina.basic.FilterSQL(txbcustomerMeetAddress.Text);

        mCustomerList.Memo = FTchina.basic.FilterSQL(txbmemo.Text);

        mCustomerList.CustomerGroupID = Convert.ToInt32(ddlcg.SelectedValue);

        mCustomerList.Callcount = Convert.ToInt32(FTchina.basic.FilterSQL(txbcallcount.Text));

        mCustomerList.MeetCount = Convert.ToInt32(FTchina.basic.FilterSQL(txbmeetCount.Text));

        mCustomerList.LastCallDate = Convert.ToDateTime(FTchina.basic.FilterSQL(txblastCallDate.Text));

        mCustomerList.LastMeetDate = Convert.ToDateTime(FTchina.basic.FilterSQL(txblastMeetDate.Text));


        FTchina.UImsg.PageRender(FTchina.BLL.CustomerListManager.ModifyCustomerListByid(mCustomerList) > 0);

    }
    private void DataAdd()
    {
        FTchina.Models.CustomerList mCustomerList = new FTchina.Models.CustomerList();

        mCustomerList.Id = Convert.ToInt32(FTchina.basic.FilterSQL(txbid.Text));

        mCustomerList.CustomerName = FTchina.basic.FilterSQL(txbcustomerName.Text);

        mCustomerList.CustormeSex = ddlsex.SelectedValue;

        mCustomerList.CustomerTel0 = FTchina.basic.FilterSQL(txbcustomerTel0.Text);
        if ((!FTchina.basic.ValidateMobile(mCustomerList.CustomerTel0)) && mCustomerList.CustomerTel0.Length > 0)
        {
            FTchina.JS.Alert("手机号码输入错误！");
            return;
        }
        mCustomerList.CustomerTel1 = FTchina.basic.FilterSQL(txbcustomerTel1.Text);

        mCustomerList.CustomerTel2 = FTchina.basic.FilterSQL(txbcustomerTel2.Text);

        mCustomerList.Customerlandline0 = FTchina.basic.FilterSQL(txbcustomerlandline0.Text);

        mCustomerList.Customerlandline1 = FTchina.basic.FilterSQL(txbcustomerlandline1.Text);

        mCustomerList.Customerlandline2 = FTchina.basic.FilterSQL(txbcustomerlandline2.Text);

        mCustomerList.CustomerAddress0 = FTchina.basic.FilterSQL(txbcustomerAddress0.Text);

        mCustomerList.CustomerMeetAddress = FTchina.basic.FilterSQL(txbcustomerMeetAddress.Text);
        mCustomerList.CustomerWork = FTchina.basic.FilterSQL(this.txbcustomerWork.Text);

        mCustomerList.Memo = FTchina.basic.FilterSQL(txbmemo.Text);

        mCustomerList.CustomerGroupID = Convert.ToInt32(ddlcg.SelectedValue);

        mCustomerList.Callcount = 0;

        mCustomerList.MeetCount = 0;

        mCustomerList.LastCallDate = new DateTime(1910, 1, 1);

        mCustomerList.LastMeetDate = new DateTime(1910, 1, 1);


        FTchina.UImsg.PageRender(FTchina.BLL.CustomerListManager.AddCustomerList(mCustomerList) > 0);
    }


    protected void btnCustomerListOk_Click(object sender, EventArgs e)
    {
        if (btnCustomerListOk.Text == "确定修改") DataModifiy();
        else DataAdd();
    }


    #region gvList显示

    private void bindData(FTchina.SearchQuery.QueryModel qm)
    {

        int pCurrentPage = (FTchina.basic.SafeRequest(Request["page"]).Length > 0) ? Convert.ToInt32(FTchina.basic.SafeRequest(Request["page"])) : 1;
        int pageSize = 15;//每页的数据
        int AllCount = Convert.ToInt32(FTchina.shujuku.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
        
        litpager.Text = FTchina.Api.Pager.GetPagerWithPageName(qm.GetTableSql, AllCount, pCurrentPage, pageSize, qm.ReturnPage);
        gvList.DataSource = FTchina.shujuku.ShowForPage(qm.TableName, qm.SqlWhere, pCurrentPage, pageSize);
        gvList.DataBind();
        txbsearch.Text = qm.Key1;

        ddlsearchcg.SelectedValue = qm.Key2;

    }
    /// <summary>
    /// 为了实现<thead> <TH>and <tbody>，满足点击表头排序功能
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void gvList_PreRender(object sender, EventArgs e)
    {
        if (gvList.Rows.Count > 0)
        {
            // 使用<TH>替换<TD> 
            gvList.UseAccessibleHeader = true;
            //This will add the <thead> and <tbody> elements 
            //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹 
            gvList.HeaderRow.TableSection = TableRowSection.TableHeader;
            // FooterRow将被<tfoot>包裹 
            gvList.FooterRow.TableSection = TableRowSection.TableFooter;
        }
    }

    #endregion





    #region GridView等控件导出到Excel
    protected void btnexcel_Click(object sender, EventArgs e)
    {
        //将分页去除
        gvList.Width = Unit.Percentage(100);
        gvList.AutoGenerateColumns = false;
        gvList.AllowPaging = false;
        string GetDateSql = string.Format(SqlTable, TableName);
        DataSet ds = FTchina.shujuku.GetDataSet(GetDateSql);
        gvList.DataSource = ds;
        gvList.DataBind();

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(stringWrite);
        gvList.RenderControl(writer);

        FTchina.Api.ToExcel.HtmlTableToExcel(basic.DealA(basic.DealINPUT(stringWrite.ToString())));
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        // 覆盖VerifyRenderingInServerForm方法，非常重要，否则系统会报错
        //base.VerifyRenderingInServerForm(control);
    }
    #endregion


    protected void btnsearch_Click(object sender, EventArgs e)
    {
        string TableName = "CustomerList";//表名
        string SqlWhere = " 1=1  ";//过滤条件

        string key2 = string.Empty;

        if (txbsearch.Text.Trim().Length > 0)
        {
            qm.Key1 = txbsearch.Text.Trim();
            SqlWhere += string.Format(SearchFilter, qm.Key1, qm.Key2);
        }
        else
        {
            qm.Key1 = "";
        }
        if (ddlsearchcg.SelectedValue == "null")
        {

            qm.Key2 = "null";
        }
        else
        {
            qm.Key2 = ddlsearchcg.SelectedValue;
            SqlWhere += " and  customergroupid in (" + qm.Key2 + ")";
        }
        string GetCountSql = "select count(id)  from CustomerList where " + SqlWhere;
        string SqlTable = "select *  from CustomerList where " + SqlWhere + "  order by id desc ";
        string ReturnPage = "CustomerList.aspx";
     //   FTchina.basic.Log(SqlTable);
        qm.TableName = TableName;
        qm.GetCountSql = GetCountSql;
        qm.GetTableSql = SqlTable;
        qm.ReturnPage = ReturnPage;
        qm.SqlWhere = SqlWhere;
        qm.WorkPage = ReturnPage;
        bindData(qm);
    }
}






